package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.adapter.SystemMessageAdapter;
import com.wuliupai.delivery.wuliupaidelivery.bean.MessageBean;
import com.wuliupai.delivery.wuliupaidelivery.constants.CommConstants;
import com.wuliupai.delivery.wuliupaidelivery.constants.Value;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * Class:系统消息列表
 * Created by KANG on 2016/11/20  14:59.
 * Email:khl8912@126.com
 */

@EActivity(R.layout.activity_system_messge_list)
public class SystemMessageActivity extends AppBaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate, AppBaseActivity.OnRefreshBtnClickListener {

    @ViewById(R.id.refreshSystemMessage)
    BGARefreshLayout refreshSystemMessage;
    @ViewById(R.id.lv_systemMessageList)
    RecyclerView lv_systemMessageList;

    private SystemMessageAdapter adapter_systemMessage;
    private List<MessageBean> list_systemMessage;
    private int startPosition;

    @AfterViews
    void startSystemMessage() {
        setDefaultTitle(this, R.string.string_system_message_title);
        bindMessageList();
        getSystamMessage(false, null);
        setListener();
    }

    /**
     * 绑定适配器
     */
    private void bindMessageList() {
        list_systemMessage = new ArrayList<>();

        adapter_systemMessage = new SystemMessageAdapter(this, list_systemMessage);
        refreshSystemMessage.setDelegate(this);
        lv_systemMessageList.setLayoutManager(new LinearLayoutManager(this));
        lv_systemMessageList.setAdapter(adapter_systemMessage);
    }

    /**
     * 获取系统消息列表
     */
    private void getSystamMessage(final boolean isClear, final BGARefreshLayout refreshLayout) {
        MessageBean messageBean = new MessageBean(String.valueOf(startPosition), String.valueOf(Value.listCount));
        String jsonMessage = JsonUtils.createJsonString(messageBean);
        try {
            String aesMessage = JsonUtils.aes(jsonMessage);
            callNet(this, "getnotificationlist", aesMessage, new NetResultOperate() {
                @Override
                public void onSuccess(String content) {

                    MessageBean parseMessage = (MessageBean) JsonUtils.parseJsonString(content, MessageBean.class);
                    if (isClear) {
                        if (null != refreshLayout) {
                            refreshLayout.endRefreshing();
                        }
                        if (!StringUtils.isEmpty(content)) {
                            list_systemMessage.clear();
                        }
                    } else if (null != refreshLayout) {//加载更多
                        refreshLayout.endLoadingMore();
                    }

                    if (!StringUtils.isEmpty(content)) {
                        list_systemMessage.clear();
                        list_systemMessage.addAll(parseMessage.infolist);
                        adapter_systemMessage.notifyDataSetChanged();
                    }
                }

                @Override
                public void onDataEmpty(String content) {
                    list_systemMessage.clear();
                    setCommenActivtiyType(CommConstants.TYPE_EMPTY, SystemMessageActivity.this);
                    adapter_systemMessage.notifyDataSetChanged();
                    if (isClear) {
                        list_systemMessage.clear();
                        if (null != refreshLayout) {
                            refreshLayout.endRefreshing();
                        }
                    }
                    if (null != refreshLayout) {
                        refreshLayout.endLoadingMore();
                    }
                }

                @Override
                public void onDataEnd(String content) {
                    refreshSystemMessage.setVisibility(View.GONE);
                }

                @Override
                public void onErr(String errorCode, String errorMsg, Context mContext) {
                    setCommenActivtiyType(CommConstants.TYPE_EMPTY, SystemMessageActivity.this);
                    super.onErr(errorCode, errorMsg, mContext);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * item的监听
     */
    private void setListener() {

        adapter_systemMessage.setOnItemClickListener(new SystemMessageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                SystemMessageDetailActivity_.intent(SystemMessageActivity.this).messageTitle(list_systemMessage.get(position).getTitle())
                        .messageTime(list_systemMessage.get(position).getSubmitTime())
                        .messageCount(list_systemMessage.get(position).getContent()).start();
            }
        });

    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        startPosition = 0;
        getSystamMessage(true, refreshLayout);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        startPosition += Value.listCount;
        getSystamMessage(false, refreshLayout);
        return false;
    }

    /**
     * 点击刷新
     */
    @Override
    public void refreshClick() {
        getSystamMessage(false, null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
